<!--  -->
<template>
  <div>
    <van-nav-bar title="登录"/>
    <van-form ref="loginForm">
      <van-cell-group>
        <van-field
          :rules="userFormRules.mobile"
          type="number"
          maxlength="11"
          v-model="user.mobile"
          name="mobile"
          left-icon="manager-o"
          placeholder="请填写手机号"
        >
          <i slot="left-icon" class="iconfont iconshouji"></i>
        </van-field>
        <van-field
          v-model="user.code"
          name="验证码"
          type="number"
          maxlength="6"
          :rules="userFormRules.code"
          placeholder="请填写验证码"
        >
          <i slot="left-icon" class="iconfont iconyanzhengma"></i>
          <template #button>
            <van-count-down
              v-if="isCountDownShow"
              :time="1000 * 5"
              format="ss s"
              @finish="isCountDownShow = false"
            />
            <van-button
              v-else
              size="small"
              class="send-sms-btn"
              @click.stop="getSmsCode"
              type="primary"
              native-type="button"
              round
            >发送验证码</van-button>
          </template>
        </van-field>
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" @click="onSubmit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
// import {login,mobile} from '@/utils/request'
import { login, mobile } from '@/api/user'
export default {
  data() {
    return {
      user: {
        mobile: '13911111111',
        code: '246810'
      },
      isCountDownShow: false,
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机号格式错误'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      }
    }
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {},
  methods: {
    async onSubmit() {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return this.$toast(err.message)
      }
      let paramObj = {
        mobile: this.user.mobile,
        code: this.user.code
      }
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '加载中...',
        forbidClick: true // 是否禁止背景点击
      })
      login.onSubmit(paramObj).then(data => {
        console.log(data)
        if (data.message === 'OK') {
          this.$toast.success('登陆成功')
          this.$store.commit('setUser', data.data)
          this.$router.back()
        }
      })
    },
    async getSmsCode() {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return this.$toast(err.message)
      }
      let mobiles = this.user.mobile
      //刚开始在请求中出现[object%20Object]说明对象中又传了一个对象，本身在api中params就是一个对象了。
      // 一开始我传值写的是({XXX:XXXX}) 类似嵌套了对象，所以url中出现了[object%20Object]
      // 在传值的时候，参数只要用（）即可，不要在用（｛｝）这种方式。
      mobile.getSmsCode(mobiles).then(data => {
        this.isCountDownShow = true
        console.log(data)
        if (data.message === 'Too many requests.') {
          this.$toast.fail('发送太频繁了，请稍后重试')
        } else {
          this.$toast('发送成功')
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
/* @import url(); 引入css类 */
.van-nav-bar {
  background-color: #3296fa;
}
/deep/.van-nav-bar__title {
  color: #fff;
}
.iconfont {
  font-size: 17px;
}
.send-sms-btn {
  background-color: #ededed;
  font-size: 12px;
  color: #666;
  border: none;
  border-radius: 10px;
}
.login-btn-wrap {
  padding: 53px 33px;
}
.login-btn {
  background-color: #6db4fb;
  border: none;
}
</style>